﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Win32;

namespace EyesSaver
{
    public partial class SettingsForm : Form
    {
        private int CurrentTime;

        public SettingsForm(Boolean startMinimized)
        {
            InitializeComponent();
            LoadSettings();
            if (startMinimized)
            {
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;
                StartHiden();
            }
        }

 
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            SaveSettings();
            StartHiden();
        }

        private void StartHiden()
        {
            StartTimer();
            Hide();
        }

        private void StartTimer()
        {
            timer1.Stop();
            timer1.Interval = (int)numericUpDown1.Value * 60 * 1000;
            timer1.Start();
            CurrentTime = 0;
        }

        private void SaveSettings()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\EyesSaver");
            regKey.SetValue("Delay", (int)numericUpDown1.Value);
            regKey.SetValue("PauseSize", (int)numericUpDown2.Value);
            regKey.SetValue("CloseDisable", CloseDissCheckBox.Checked);
        }

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void workTimer_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            NotificationForm frm = new NotificationForm((int)numericUpDown2.Value * 60 * 1000, CloseDissCheckBox.Checked);
            frm.ShowDialog();
            timer1.Interval = (int)numericUpDown1.Value * 60 * 1000;
            timer1.Start();
            CurrentTime = 0;
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                CurrentTime += timer2.Interval;
                int time = (timer1.Interval - CurrentTime) / 1000; 
                NotifyIcon.Text = "Time left to relax: "+(time / 60).ToString()
                + ":" + String.Format("{0:00}", (time % 60));
            }
            else
            {
                NotifyIcon.Text = "";
            }
        }


        private void LoadSettings()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\EyesSaver");
            numericUpDown1.Value = (int)regKey.GetValue("Delay", 40);
            numericUpDown2.Value = (int)regKey.GetValue("PauseSize", 5);
            String val = (String)regKey.GetValue("CloseDisable", "False");
            CloseDissCheckBox.Checked = val.ToLower().CompareTo("true") == 0;
        }

        private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

    }
}
